<template>
  <div class="app-container">
    <!--条件搜索表单-->
    <el-row :gutter="20">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-select v-model="searchObj.payStatus" placeholder="支付状态" clearable @change="getAll()">
            <el-option v-for="item in payStatusOptions" :key="item.value" :value="item.value" :label="item.text" />
          </el-select>
        </el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getAll()">查询</el-button>
        <el-button icon="el-icon-refresh" @click="reset()">清空</el-button>
      </el-form>
    </el-row>

    <el-table
      v-loading="loading"
      element-loading-text="正在努力加载数据中..."
      :data="page.list"
      :border="false"
      stripe
      style="width: 100%; margin: 15px 0px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="index" fixed="left" width="80" label="序号" />
      <el-table-column prop="payNo" fixed="left" label="支付编号" min-width="100" sortable show-overflow-tooltip />
      <el-table-column prop="orderNo" fixed="left" label="订单编号" min-width="100" sortable show-overflow-tooltip />
      <el-table-column prop="tradeNo" label="交易流水号" min-width="100" sortable show-overflow-tooltip />
      <el-table-column prop="payMethod" label="支付方式" min-width="100" sortable>
        <template v-slot="{ row }">
          <el-tag v-if="row.payMethod == 1" effect="plain">支付宝</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="amount" label="支付金额" min-width="100" sortable show-overflow-tooltip>
        <template v-slot="{ row }">
          {{ row.amount + ' 元' }}
        </template>
      </el-table-column>
      <el-table-column prop="payStatus" label="支付状态" min-width="100" sortable>
        <template v-slot="{ row }">
          <template v-for="item in payStatusOptions">
            <el-tag v-if="row.payStatus == item.value" :type="item.style" effect="plain">{{ item.text }}</el-tag>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="payTime" label="支付时间" min-width="100" sortable show-overflow-tooltip />
      <el-table-column prop="createTime" label="创建时间" min-width="100" sortable show-overflow-tooltip />
    </el-table>

    <!-- 分页导航栏 -->
    <div>
      <el-pagination
        :background="true"
        :current-page="pIndex"
        :page-size="pSize"
        :total="page.total"
        :page-sizes="[1, 3, 6, 9, 12, 15, 18]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        @current-change="getAll"
        @size-change="handleSizeChange"
      />
    </div>
  </div>
</template>

<script>
// 引入api接口定义的js文件
import paymentApi from '@/api/payment'

export default {
  data() {
    // 定义变量和初始值
    return {
      payStatusOptions: [
        { value: 0, text: '未支付', style: 'primary' },
        { value: 1, text: '已支付', style: 'success' },
        { value: 2, text: '支付失败', style: 'warning' },
        { value: 3, text: '已退款', style: 'danger' }
      ],
      // 查询条件
      searchObj: {},

      // 列表相关
      loading: false, // 加载状态
      pIndex: 1, // 当前页
      pSize: 6, // 每页显示记录数
      page: [] // 列表
    }
  },
  created() {
    this.getAll()
  },
  methods: {
    // 加载列表数据
    getAll(current = 1) {
      this.loading = true
      this.pIndex = current
      paymentApi
        .getPage(this.pIndex, this.pSize, this.searchObj)
        .then((response) => {
          this.page = response.data
        })
        .finally(() => {
          this.loading = false
        })
    },

    // 行内筛选
    filterHandler(value, row, column) {
      const property = column['property']
      return row[property] === value
    },

    // 复选框选中
    handleSelectionChange(r) {
      // 得到选中的id
      this.ids = r.map((v) => v.id)
    },

    // 改变页码
    handleSizeChange(pageSize) {
      this.pSize = pageSize
      this.getAll()
    },

    // 重置
    reset() {
      this.searchObj = {}
      this.getAll()
    }
  }
}
</script>

<style></style>
